var User = function(users) {
	this.users = users;
}
var staffModel = function(userId, userName, userLName, userFName, userEmail, userAddr, userPhone, userRole, banned) {
	this.userId = userId;
	this.userName = userName;
	this.userLName = userLName;
	this.userFName = userFName;
	this.userEmail = userEmail;
	this.userAddr = userAddr;
	this.userPhone = userPhone;
	this.userRole = userRole;
	this.banned = banned;
}
function createStaffModel(user) {
	var staff = user.getElementsByTagName("User");
	var arrStaff = [];
	var staffId;
	var staffUsername;
	var staffLName;
	var staffFName;
	//var staffAvatar;
	var staffPhone;
	var staffAddress;
	var staffRole;
	var staffIsBan;

	for (var i = 0; i < staff.length; i++) {
		staffId = staff[i].getAttribute("userId");
		staffRole = staff[i].getAttribute("role");
		staffUsername = staff[i].getElementsByTagName("userName")[0].childNodes[0].nodeValue;
		staffLName = staff[i].getElementsByTagName("lastName")[0].childNodes[0].nodeValue;
		staffFName = staff[i].getElementsByTagName("firstName")[0].childNodes[0].nodeValue;
		//staffAvatar = staff[i].getElementsByTagName("avatar")[0].childNodes[0].nodeValue;
		staffPhone = staff[i].getElementsByTagName("phone")[0].childNodes[0].nodeValue;
		staffAddress = staff[i].getElementsByTagName("address")[0].childNodes[0].nodeValue;
		staffIsBan = staff[i].getElementsByTagName("isBanned")[0].childNodes[0].nodeValue;
		staffEmail = staff[i].getElementsByTagName("email")[0].childNodes[0].nodeValue;
		arrStaff[i] = {
			"staffId" : staffId,
			"staffRole" : staffRole,
			"staffUsername" : staffUsername,
			"staffLName" : staffLName,
			"staffFName" : staffFName,
			
			"staffPhone" : staffPhone,
			"staffAddress" : staffAddress,
			"staffIsBan" : staffIsBan,
			"staffEmail" : staffEmail
		}
	}

	return new User(arrStaff);
}

function showManageStaffPage(arrUsers) {
	var key = "user";
	addActive(key);
	var rootNode = $id("loadUser");
	rootNode.innerHTML = "";
	if (arrUsers.length > 0) {
		var nodeTable = createTable();
		nodeTable.setAttribute("id", "tableStaff");

		var nodeThId = createNode("th");
		var idTh = createTextNode("Mã nhân viên");
		nodeThId.appendChild(idTh);
		var nodeThUName = createNode("th");
		var uNameTh = createTextNode("Tài khoản");
		nodeThUName.appendChild(uNameTh);
		var nodeThLName = createNode("th");
		var lNameTh = createTextNode("Họ");
		nodeThLName.appendChild(lNameTh);
		var nodeThFName = createNode("th");
		var fNameTh = createTextNode("Tên");
		nodeThFName.appendChild(fNameTh);
		var nodeThEmail = createNode("th");
		var emailTh = createTextNode("E-mail");
		nodeThEmail.appendChild(emailTh);
		var nodeThAdd = createNode("th");
		var addTh = createTextNode("Địa chỉ");
		nodeThAdd.appendChild(addTh);
		var nodeThPhone = createNode("th");
		var phoneTh = createTextNode("Điện thoại");
		nodeThPhone.appendChild(phoneTh);
		var nodeThRole = createNode("th");
		var roleTh = createTextNode("Role");
		nodeThRole.appendChild(roleTh);
		var nodeThBan = createNode("th");
		var banTh = createTextNode("Banned");
		nodeThBan.appendChild(banTh);
		var nodeThUpdate = createNode("th");
		var nodeThDelete = createNode("th");

		nodeTable.appendChild(nodeThId);
		nodeTable.appendChild(nodeThUName);
		nodeTable.appendChild(nodeThLName);
		nodeTable.appendChild(nodeThFName);
		nodeTable.appendChild(nodeThEmail);
		nodeTable.appendChild(nodeThAdd);
		nodeTable.appendChild(nodeThPhone);
		nodeTable.appendChild(nodeThRole);
		nodeTable.appendChild(nodeThBan);
		nodeTable.appendChild(nodeThUpdate);
		nodeTable.appendChild(nodeThDelete);

		var nodeTr = null;
		var user = null;
		var nodeId = null;
		var nodeUserName = null;
		var nodeLName = null;
		var nodeFName = null;
		var nodeEmail = null;
		var nodeAdd = null;
		var nodePhone = null;
		var nodeRole = null;
		var nodeBan = null;
		var nodeTdUpdate = null;
		var nodeDivUpdate = null;
		var nodeUpdate = null;
		var nodeTdDelete = null;
		var nodeDivDelete = null;
		var nodeDelete = null;

		var length = arrUsers.length;

		for (var i = 0; i < length; i++) {
			user = arrUsers[i];
			nodeTr = createNode("tr");
			nodeTr.setAttribute("id", "userIdInRow" + user.staffId);

			nodeId = createNode("td");
			var id = createTextNode(user.staffId);
			nodeId.appendChild(id);

			nodeUserName = createNode("td");
			var uname = createTextNode(user.staffUsername);
			nodeUserName.appendChild(uname);

			nodeLName = createNode("td");
			var lname = createTextNode(user.staffLName);
			nodeLName.appendChild(lname);

			nodeFName = createNode("td");
			var fname = createTextNode(user.staffFName);
			nodeFName.appendChild(fname);

			nodeEmail = createNode("td");
			var email = createTextNode(user.staffEmail);
			nodeEmail.appendChild(email);

			nodeAdd = createNode("td");
			var addr = createTextNode(user.staffAddress);
			nodeAdd.appendChild(addr);

			nodePhone = createNode("td");
			var phone = createTextNode(user.staffPhone);
			nodePhone.appendChild(phone);

			nodeRole = createNode("td");
			var role = createTextNode(user.staffRole);
			nodeRole.appendChild(role);

			nodeBan = createNode("td");
			var ban = createTextNode(user.staffIsBan);
			nodeBan.appendChild(ban);

			//create node update
			nodeTdUpdate = createNode("td");
			nodeDivUpdate = createDiv();
			nodeTdUpdate.addEventListener("click", function(event) {
				var currentRow = $id(event.currentTarget.parentNode.id);
				var cels = currentRow.getElementsByTagName("td");
				var userId = cels[0].innerText;
				var userName = cels[1].innerText;
				var userLName = cels[2].innerText;
				var userFName = cels[3].innerText;
				var userEmail = cels[4].innerText;
				var userAddr = cels[5].innerText;
				var userPhone = cels[6].innerText;
				var userRole = cels[7].innerText;
				var banned = cels[8].innerText;

				var model = new staffModel(userId, userName, userLName, userFName, userEmail, userAddr, userPhone, userRole, banned);
				showPage("content-updateUser");
				createUpdateUserView(model.userId, model.userName, model.userLName, model.userFName, model.userEmail, model.userAddr, model.userPhone, model.userRole, model.banned);

			});
			nodeUpdate = createNode("img");
			nodeUpdate.setAttribute("src", "Image/update.png");
			nodeUpdate.style.cursor = "pointer";
			nodeDivUpdate.appendChild(nodeUpdate);
			nodeTdUpdate.appendChild(nodeDivUpdate);

			//create node delete
			nodeTdDelete = createNode("td");
			nodeDivDelete = createDiv();
			var userLogin = $id("adminId").innerText;
			if (uname.data != userLogin) {
				nodeDelete = createNode("img");
				nodeDelete.setAttribute("src", "Image/delete_icon.png");
				nodeDelete.style.cursor = "pointer";
				nodeDivDelete.appendChild(nodeDelete);

				nodeTdDelete.addEventListener("click", function(event) {
					var row = $id(event.currentTarget.parentNode.id);
					var cels = row.getElementsByTagName("td");
					var uId = cels[0].innerText;
					var uName = cels[1].innerText;
					var r = confirm("Bạn có muốn xóa user có tài khoản " + uName + "?");

					if (r == true) {
						delStaff(uId, uName);
					}
				});
			} 

			nodeTdDelete.appendChild(nodeDivDelete);

			nodeTr.appendChild(nodeId);
			nodeTr.appendChild(nodeUserName);
			nodeTr.appendChild(nodeLName);
			nodeTr.appendChild(nodeFName);
			nodeTr.appendChild(nodeEmail);
			nodeTr.appendChild(nodeAdd);
			nodeTr.appendChild(nodePhone);
			nodeTr.appendChild(nodeRole);
			nodeTr.appendChild(nodeBan);
			nodeTr.appendChild(nodeTdUpdate);
			nodeTr.appendChild(nodeTdDelete);

			nodeTable.appendChild(nodeTr);
		}
		rootNode.appendChild(nodeTable);
		var divPaging = createDiv();
		divPaging.setAttribute("id", "pageStaff");
		divPaging.setAttribute("class", "paging");
		var script = createNode("script");
		script.setAttribute("type", "text/javascript");
		script.innerHTML = "var pager = new Pager('tableStaff', 10); pager.init(); pager.showPageNav('pager', 'pageStaff'); pager.showPage(1);";
		rootNode.appendChild(divPaging);
		rootNode.appendChild(script);
	}
}

function callServiceStaff() {

	ajax.get({
		url : "http://localhost:8080/book-service/admin/manageStaff",
		success : function(data, xhr) {
			showPage("content-manageUser");
			var model = createStaffModel(data);
			if (model.users.length <= 0) {
				alert("Không có kết quả!");
			}
			showManageStaffPage(model.users);
		},
		failure : function(xhr) {
			if (xhr.status == 401) {
				alert("Bạn không có quyền truy cập vào trang này!");

			} else {
				alert("Có lỗi xảy ra. Xin thử lại!");
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});

}

function getAllAdmin() {

	ajax.get({
		url : "http://localhost:8080/book-service/admin/manageStaff/getAllAdmin",
		success : function(data, xhr) {
			var model = createStaffModel(data);
			if (model.users.length <= 0) {
				alert("Không có kết quả!");
			}
			showManageStaffPage(model.users);
		},
		failure : function(xhr) {
			if (xhr.status == 401) {
				alert("Bạn không có quyền truy cập vào trang này!");

			} else {
				alert("Có lỗi xảy ra. Xin thử lại!");
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
}

function getAllStaff() {

	ajax.get({
		url : "http://localhost:8080/book-service/admin/manageStaff/getAllStaff",
		success : function(data, xhr) {
			var model = createStaffModel(data);
			if (model.users.length <= 0) {
				alert("Không có kết quả!");
			}
			showManageStaffPage(model.users);
		},
		failure : function(xhr) {
			if (xhr.status == 401) {
				alert("Bạn không có quyền truy cập vào trang này!");

			} else {
				alert("Có lỗi xảy ra. Xin thử lại!");
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
}

function searchStaff() {
	var txtSearch = document.getElementById("txtSearchStaff").value;
	ajax.get({
		url : "http://localhost:8080/book-service/admin/manageStaff/searchStaff",
		param : {
			"txtUserName" : txtSearch
		},
		success : function(data, xhr) {
			var model = createStaffModel(data);
			if (model.users.length <= 0) {
				alert("Không có kết quả!");
			}
			showManageStaffPage(model.users);
		},
		failure : function(xhr) {
			if (xhr.status == 401) {
				alert("Bạn không có quyền truy cập vào trang này!");

			} else {
				alert("Có lỗi xảy ra. Xin thử lại!");
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
}

function delStaff(uId, uName) {
	ajax.del({
		url : "http://localhost:8080/book-service/admin/manageStaff",
		param : {
			"txtUserId" : uId
		},
		success : function() {
			alert("Xóa user có tài khoản " + uName + " thành công");
			showPage("content-manageUser");
			callServiceStaff();
		},
		failure : function(xhr) {
			if (xhr.status == 401) {
				alert("Bạn không có quyền truy cập vào trang này!\n");
			} else {
				alert("Có lỗi xảy ra. Xin thử lại!");
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
}

